from django.http import HttpResponse
from django.shortcuts import render, redirect
from .. import models
from ..utils.pagination import Pagination


def depart_list(request):
    """部门列表"""
    # 去数据库中获取所有的部门列表
    # [对象，对象，对象]
    queryset = models.Department.objects.all()

    page_object = Pagination(request, queryset, page_size=2)
    context = {
        "queryset": page_object.page_queryset,
        "page_string": page_object.html()
    }
    return render(request, "app_02/depart_list.html", context)


def depart_add(request):
    """添加部门"""
    if request.method == "GET":
        return render(request, "app_02/depart_add.html")

    # 获取用户POST提交过来的数据
    title = request.POST.get("title")

    # 保存到数据库  默认获取name的值
    models.Department.objects.create(title=title)

    # 重定向回部门列表
    return redirect("/app_02/depart/list")


def depart_delete(request):
    """删除部门"""
    # http://127.0.0,1:8000/depart/delete/?nid=1
    # 获取ID
    nid = request.GET.get("nid")

    # 删除
    models.Department.objects.filter(id=nid).delete()

    # 重定向回部门列表
    return redirect("/app_02/depart/list")


def depart_edit(request, nid):
    # http://127.0.0.1:8000/app_02/depart/8/edit/
    # http://127.0.0.1:8000/app_02/depart/9/edit/
    # http://127.0.0.1:8000/app_02/depart/10/edit/
    """编辑部门"""

    if request.method == "GET":
        # 根据nid获取其他的数据[对象,]
        row_object = models.Department.objects.filter(id=nid).first()
        # print(row_object.id, row_object.title)
        return render(request, "app_02/depart_edit.html", {"row_object": row_object})

    # 获取用户提交的数据
    title = request.POST.get("title")

    # 根据ID找到数据库中的数据并进行更新
    models.Department.objects.filter(id=nid).update(title=title)

    # 重定向回部门列表
    return redirect("/app_02/depart/list")


def depart_multi(request):
    """批量上传(Excel文件)"""
    from openpyxl import load_workbook
    file_object = request.FILES.get("exc")

    # 1. 获取用户上传的文件对象
    # print (type(file_object))

    # 2. 对象传递给openpyxl, 由openpyxl读取文件的内容
    wb = load_workbook(file_object)
    sheet = wb.worksheets[0]

    # cell = sheet.cell(1, 1)
    # print(cell.value)
    for row in sheet.iter_rows(min_row=2):
        text = row[0].value
        exists = models.Department.objects.filter(title=text).exists()
        if not exists:
            models.Department.objects.create(title=text)


    return redirect("/app_02/depart/list/")